package com.kaimy.model 
{
	import com.kaimy.controller.LoadController;
	import com.kaimy.model.constants.ConstructTypes;
	import com.kaimy.model.factory.ModelFactory;
	import com.kaimy.model.vo.ModelVO;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	/**
	 * @author jbonfante
	date: Aug 2, 2009
	for: Roundarch Inc.
 
	Document Information:
	Project: KaimysAwesomeFlashProject
	Package: com.kaimy.model.OpenModel
	 */
	public class OpenModel extends EventDispatcher
	{
		private var _model : XML;
		private var factory : ModelFactory = ModelFactory.getInstance();
		private var _imageDescriptor : ImageDescriptor;
		private var loadController : LoadController = LoadController.getInstance();
		private var _imageArray : Array = [];

		public function OpenModel(model : XML, target : IEventDispatcher = null)
		{
			_model = model;
			Parse();
			super(target);
		}

		private function Parse() : void
		{
			
			_imageDescriptor = new ImageDescriptor(_model.images.@name, _model.images.@directory, _model.images.@url);
		
			loadController.add(_imageDescriptor.url, {id:_imageDescriptor.name}).addEventListener(Event.COMPLETE, ParseImages, false, 0, true);
			loadController.start();
			/**
			 * directory="images/dotprobe.n/"
		name="Structure"
		url="data/ImagesStructure.xml" />
			 */
		}

		private function ParseImages(event : Event) : void
		{
			var images : XML = loadController.getXML(_imageDescriptor.name);
			var totalImages : Number = Number(images.file.length());
			//var imagesToLoadTotal:Number = totalImages * images.types.construct.length();
			var parsedImages : Number = 0;
			//var namingConvention:String = String(images.@namespace);
			//var extension:String = String(images.@type);
			//var delimeter:String = String(images.@delimeter);
			var imageModel : ModelVO;
			var props : Object = new Object();
			//var construct : XML;
			var image : String;
			var regEx : RegExp = /.*_(..)_(.)/;	
			for(var i : int = 0;i <		 totalImages;i++)
			{
				image = images.file[i];
				var aData : Object = regEx.exec(image);
				var imageSet : Number = Number(image.substr(0, 2));
						
				var constructDefinition : ConstructDefinition = ConstructTypes.evalDef(String(aData[1]));
							
				//construct = images.types.construct[o];
				props.count = imageSet;
				props.constructType = constructDefinition.constuctType;
				props.constructName = constructDefinition.constructName;
				props.imageName = constructDefinition.imageName;
				props.imageType = constructDefinition.imageType;
				props.imageDef = constructDefinition.imageDef;
				props.url = image;
				imageModel = factory.createVO(props);
				_imageArray.push(imageModel);
				parsedImages++;
					
				TestCount(parsedImages, totalImages);
			}
		}

		private function TestCount(parsedImages : Number, totalImages : Number) : void
		{
			trace(parsedImages);
			if(parsedImages == totalImages)
			{
				trace("factory Complete");
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}

		public function get imageArray() : Array
		{
			return _imageArray;
		}

		public function get imageDescriptor() : ImageDescriptor
		{
			return _imageDescriptor;
		}
	}
}
